package com.yanqu.road.dao.impl.servercenter;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.servercenter.DAServerInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DAServerInfoDaoImpl extends TempDao {

    private static String getDAServerInfoSql = "select * from `t_s_data_analytics_server_info`";
    public void getDAServerInfoList(Map<Integer, List<DAServerInfo>> serverListMap) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getDAServerInfoSql);
            rs = call.executeQuery();
            while (rs.next()) {
                DAServerInfo data = new DAServerInfo();
                data.setChannelId(rs.getInt("channelId"));
                data.setServerId(rs.getLong("serverId"));
                String str = rs.getString("serverList");
                if(!StringUtils.isNullOrEmpty(str)){
                    data.setServerList(StringUtils.stringToLongList(str, ","));
                }
                data.setEndServerId(rs.getLong("endServerId"));
                if(!serverListMap.containsKey(data.getChannelId())){
                    serverListMap.put(data.getChannelId(), new ArrayList<>());
                }
                serverListMap.get(data.getChannelId()).add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
    }

}
